package com.cqs.leetcode.math;

import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

public class MissingTwoLCCI {

    public int[] missingTwo(int[] nums) {
        long sum1 = 0;
        for (int i = 1; i <= nums.length + 2; i++) {
            sum1 += i;
        }

        for (int num : nums) {
            sum1 -= num;
        }

        long sum2 = 0;
        for (int i = 1; i <= nums.length + 2; i++) {
            sum2 += i * i;
        }

        for (int num : nums) {
            sum2 -= num * num;
        }

        int x = 1;
        for (int i = 1; i < sum1; i++) {
            if (i * i + (sum1 - i) * (sum1 - i) == sum2) {
                x = i;
                break;
            }
        }
        return new int[]{x, (int) (sum1 - x)};
    }

    public static void main(String[] args) {
        MissingTwoLCCI missingTwoLCCI = new MissingTwoLCCI();
        List<int[]> cases = new ArrayList<>();
        cases.add(new int[]{1});
        cases.add(new int[]{2,3});
        cases.add(new int[]{1,3,4,5,6,9,8,7});
        cases.forEach(new Consumer<int[]>() {
            @Override
            public void accept(int[] ints) {
                System.out.println(Lists.newArrayList(Arrays.stream(ints).boxed().collect(toList())) + "\t" + Arrays.stream(missingTwoLCCI.missingTwo(ints)).boxed().collect(Collectors.toList()));
            }
        });
    }
}
